package com.lebang.controller.sys;

import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.component.RedisClient;
import com.lebang.domain.AppEarnRouseSort;
import com.lebang.domain.TaskRouse;
import com.lebang.example.AppEarnRouseSortExample;
import com.lebang.service.AppEarnRouseSortService;
import com.lebang.service.NewRouseService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 数据源排序
 *
 * @author hwq
 * @date 2020-03-13
 */
@Controller
@RequestMapping("/sys/appearnrousesort")
public class AppEarnRouseSortController {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private AppEarnRouseSortService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @GetMapping("/list")
    public String list() {

        return "/appearnrousesort/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model, HttpSession session) {
        AppEarnRouseSort vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppEarnRouseSort();
            vo.setSdkKey("CSJ");
            vo.setType(1);
        }
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("vo", rouses);
        model.addAttribute("item", vo);
        return "/appearnrousesort/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(AppEarnRouseSort vo) {
        if (vo.getId() != null) {
            AppEarnRouseSort qo=baseService.selectById(vo.getId());
            vo.setModifyTime(LocalDateTime.now());

            baseService.updateById(vo);
            baseService.init(qo.getAppStore());
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        baseService.init(vo.getAppStore());
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        AppEarnRouseSort vo = baseService.selectById(id);
        baseService.deleteById(id);
        baseService.init(vo.getAppStore());
        return Callback.success("success", "删除成功");
    }
    @Login
    @PostMapping("/init")
    @ResponseBody
    public JsonData init(String appStore) {

        baseService.init(appStore);
        return Callback.success("success", "加载成功");
    }

    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map<String, Object> datalist(HttpSession session
            , @RequestParam(required = false) String appStore
            , Long page, Integer limit) {
        AppEarnRouseSortExample example = new AppEarnRouseSortExample();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        AppEarnRouseSortExample.Criteria criteria = example.createCriteria();
        if(newRouseService.isRoot(session)){
            criteria.andSdkKeyNotIn(sdkList);
        }
        if (!TextUtil.isEmpty(appStore)){
            criteria.andAppStoreEqualTo(appStore);
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", baseService.selectByExample(example));
        returnMap.put("count", baseService.countByExample(example));
        returnMap.put("code", 0);
        return returnMap;

    }


}
